package com.mmall.Concurrency;

import com.mmall.Concurrency.example.ThreadLocal.RequestHolder;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 *  在Filter过滤器中，add初始要加载的值
 */
@Slf4j
public class HttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;  //强行转化
        log.info("do filter,{},{}",Thread.currentThread().getId(),request.getServletPath());
      //  request.getSession().getAttribute("user");
        //直接用public类的静态方法
        RequestHolder.add(Thread.currentThread().getId());
        //只是数据处理，保障拦截器执行完，请求继续被处理
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
